﻿/* 自定义的Key相关函数的模块class版本,在最后导出MyKey对象.用class的好处是不需要再对函数对象使用call,缩短代码
*/

class __MyKey2 {

  ;按键后放开按键函数, key为按键, pressDuration为持续时间
  press(key, pressDuration := 100) {
    send, {%key% down}
    sleep, %pressDuration%
    send, {%key% up}
  }

  ;连续按键函数,keys为按键[],delays为延时[]长度应该比keys小1,duration则是全部按下之后持续的时间
  pressKeys(keys, delays, duration := 50){
    for index, val in keys{
      send {%val% down}
      if (index < keys.Count())
        sleep, % delays[index]
    }
    sleep, %duration%
    for index, val in keys{
      send {%val% up}
    }
  }
  
  ;等待按键key放开,等待waitTime毫秒,每waitDuration毫秒检查一次,若发现按键放开返回1,等待完毕未放开返回0
  waitKeyUp(key, waitTime, waitDuration := 100){
    temp := 0 ;计算总等待时间
    delta := 0 ;下一次等待时间
    while (temp < waitTime) {
      delta := (waitTime-temp > waitDuration) ? (waitDuration) : (waitTime-temp)
      if (!GetKeyState(key)) {
        return 1
      }
      sleep ,%delta%
      temp := temp+delta
    }
    return 0
  }
  
  ;连续输入指令,相邻的两个key有重叠时间,等前一个key放开时,后面的key才会输入
  inputSkill(keys, delays){
    for i, v in keys {
      curKey := v
      curDelay := delays[i]
      if (i > 1)
        lastKey := keys[i-1]
      else
        lastKey := 0
      send {%curKey% down}
      sleep, %curDelay%
      if lastKey
        send {%lastKey% up}
      if (i = keys.Count())
        send {%curKey% up}
      sleep, 30 ;放开前一个按键与输入后一个按键之间需要有一点时间缓冲
    }
  }
  
}

/* 导出MyKey, 打包函数
*/
global MyKey := new __MyKey2()
